

#SWEDEN


#FLAG

# INTELLIGENT

# 1. intelligent1_se_diff ~ treat_flag
model_intelligent1_se_diff_flag <- lm(intelligent1_se_diff ~ treat_flag, data = df)
summary(model_intelligent1_se_diff_flag)
margins_intelligent1_se_diff_flag <- margins(model_intelligent1_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent1_se_diff_flag)

# 2. intelligent2_se_diff ~ treat_flag
model_intelligent2_se_diff_flag <- lm(intelligent2_se_diff ~ treat_flag, data = df)
summary(model_intelligent2_se_diff_flag)
margins_intelligent2_se_diff_flag <- margins(model_intelligent2_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent2_se_diff_flag)

# 3. intelligent3_se_diff ~ treat_flag
model_intelligent3_se_diff_flag <- lm(intelligent3_se_diff ~ treat_flag, data = df)
summary(model_intelligent3_se_diff_flag)
margins_intelligent3_se_diff_flag <- margins(model_intelligent3_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent3_se_diff_flag)

# 4. intelligent4_se_diff ~ treat_flag
model_intelligent4_se_diff_flag <- lm(intelligent4_se_diff ~ treat_flag, data = df)
summary(model_intelligent4_se_diff_flag)
margins_intelligent4_se_diff_flag <- margins(model_intelligent4_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent4_se_diff_flag)

# 5. intelligent5_se_diff ~ treat_flag
model_intelligent5_se_diff_flag <- lm(intelligent5_se_diff ~ treat_flag, data = df)
summary(model_intelligent5_se_diff_flag)
margins_intelligent5_se_diff_flag <- margins(model_intelligent5_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent5_se_diff_flag)

# 6. intelligent6_se_diff ~ treat_flag
model_intelligent6_se_diff_flag <- lm(intelligent6_se_diff ~ treat_flag, data = df)
summary(model_intelligent6_se_diff_flag)
margins_intelligent6_se_diff_flag <- margins(model_intelligent6_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent6_se_diff_flag)

# 7. intelligent7_se_diff ~ treat_flag
model_intelligent7_se_diff_flag <- lm(intelligent7_se_diff ~ treat_flag, data = df)
summary(model_intelligent7_se_diff_flag)
margins_intelligent7_se_diff_flag <- margins(model_intelligent7_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent7_se_diff_flag)

# 8. intelligent8_se_diff ~ treat_flag
model_intelligent8_se_diff_flag <- lm(intelligent8_se_diff ~ treat_flag, data = df)
summary(model_intelligent8_se_diff_flag)
margins_intelligent8_se_diff_flag <- margins(model_intelligent8_se_diff_flag, variables = "treat_flag")
summary(margins_intelligent8_se_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_flag_diff <- list(
  broom::tidy(model_intelligent1_se_diff_flag) %>% mutate(outcome = "S"),
  broom::tidy(model_intelligent2_se_diff_flag) %>% mutate(outcome = "V"),
  broom::tidy(model_intelligent3_se_diff_flag) %>% mutate(outcome = "MP"),
  broom::tidy(model_intelligent4_se_diff_flag) %>% mutate(outcome = "C"),
  broom::tidy(model_intelligent5_se_diff_flag) %>% mutate(outcome = "L"),
  broom::tidy(model_intelligent6_se_diff_flag) %>% mutate(outcome = "M"),
  broom::tidy(model_intelligent7_se_diff_flag) %>% mutate(outcome = "KD"),
  broom::tidy(model_intelligent8_se_diff_flag) %>% mutate(outcome = "SD")
)

coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
library(ggplot2)

flag_t1_se <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Intelligent by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag intelligent by party SE.jpeg",
  plot     = flag_t1_se
) 
#############################################################

#RELIABLE

# reliable

# 1. reliable1_se_diff ~ treat_flag
model_reliable1_se_diff_flag <- lm(reliable1_se_diff ~ treat_flag, data = df)
summary(model_reliable1_se_diff_flag)
margins_reliable1_se_diff_flag <- margins(model_reliable1_se_diff_flag, variables = "treat_flag")
summary(margins_reliable1_se_diff_flag)

# 2. reliable2_se_diff ~ treat_flag
model_reliable2_se_diff_flag <- lm(reliable2_se_diff ~ treat_flag, data = df)
summary(model_reliable2_se_diff_flag)
margins_reliable2_se_diff_flag <- margins(model_reliable2_se_diff_flag, variables = "treat_flag")
summary(margins_reliable2_se_diff_flag)

# 3. reliable3_se_diff ~ treat_flag
model_reliable3_se_diff_flag <- lm(reliable3_se_diff ~ treat_flag, data = df)
summary(model_reliable3_se_diff_flag)
margins_reliable3_se_diff_flag <- margins(model_reliable3_se_diff_flag, variables = "treat_flag")
summary(margins_reliable3_se_diff_flag)

# 4. reliable4_se_diff ~ treat_flag
model_reliable4_se_diff_flag <- lm(reliable4_se_diff ~ treat_flag, data = df)
summary(model_reliable4_se_diff_flag)
margins_reliable4_se_diff_flag <- margins(model_reliable4_se_diff_flag, variables = "treat_flag")
summary(margins_reliable4_se_diff_flag)

# 5. reliable5_se_diff ~ treat_flag
model_reliable5_se_diff_flag <- lm(reliable5_se_diff ~ treat_flag, data = df)
summary(model_reliable5_se_diff_flag)
margins_reliable5_se_diff_flag <- margins(model_reliable5_se_diff_flag, variables = "treat_flag")
summary(margins_reliable5_se_diff_flag)

# 6. reliable6_se_diff ~ treat_flag
model_reliable6_se_diff_flag <- lm(reliable6_se_diff ~ treat_flag, data = df)
summary(model_reliable6_se_diff_flag)
margins_reliable6_se_diff_flag <- margins(model_reliable6_se_diff_flag, variables = "treat_flag")
summary(margins_reliable6_se_diff_flag)

# 7. reliable7_se_diff ~ treat_flag
model_reliable7_se_diff_flag <- lm(reliable7_se_diff ~ treat_flag, data = df)
summary(model_reliable7_se_diff_flag)
margins_reliable7_se_diff_flag <- margins(model_reliable7_se_diff_flag, variables = "treat_flag")
summary(margins_reliable7_se_diff_flag)

# 8. reliable8_se_diff ~ treat_flag
model_reliable8_se_diff_flag <- lm(reliable8_se_diff ~ treat_flag, data = df)
summary(model_reliable8_se_diff_flag)
margins_reliable8_se_diff_flag <- margins(model_reliable8_se_diff_flag, variables = "treat_flag")
summary(margins_reliable8_se_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_flag_diff <- list(
  broom::tidy(model_reliable1_se_diff_flag) %>% mutate(outcome = "S"),
  broom::tidy(model_reliable2_se_diff_flag) %>% mutate(outcome = "V"),
  broom::tidy(model_reliable3_se_diff_flag) %>% mutate(outcome = "MP"),
  broom::tidy(model_reliable4_se_diff_flag) %>% mutate(outcome = "C"),
  broom::tidy(model_reliable5_se_diff_flag) %>% mutate(outcome = "L"),
  broom::tidy(model_reliable6_se_diff_flag) %>% mutate(outcome = "M"),
  broom::tidy(model_reliable7_se_diff_flag) %>% mutate(outcome = "KD"),
  broom::tidy(model_reliable8_se_diff_flag) %>% mutate(outcome = "SD")
)

coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
library(ggplot2)

flag_t2_se <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Reliable by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag reliable by party SE.jpeg",
  plot     = flag_t2_se
)
####################################################

#SELFISH
# selfish

# 1. selfish1_se_diff ~ treat_flag
model_selfish1_se_diff_flag <- lm(selfish1_se_diff ~ treat_flag, data = df)
summary(model_selfish1_se_diff_flag)
margins_selfish1_se_diff_flag <- margins(model_selfish1_se_diff_flag, variables = "treat_flag")
summary(margins_selfish1_se_diff_flag)

# 2. selfish2_se_diff ~ treat_flag
model_selfish2_se_diff_flag <- lm(selfish2_se_diff ~ treat_flag, data = df)
summary(model_selfish2_se_diff_flag)
margins_selfish2_se_diff_flag <- margins(model_selfish2_se_diff_flag, variables = "treat_flag")
summary(margins_selfish2_se_diff_flag)

# 3. selfish3_se_diff ~ treat_flag
model_selfish3_se_diff_flag <- lm(selfish3_se_diff ~ treat_flag, data = df)
summary(model_selfish3_se_diff_flag)
margins_selfish3_se_diff_flag <- margins(model_selfish3_se_diff_flag, variables = "treat_flag")
summary(margins_selfish3_se_diff_flag)

# 4. selfish4_se_diff ~ treat_flag
model_selfish4_se_diff_flag <- lm(selfish4_se_diff ~ treat_flag, data = df)
summary(model_selfish4_se_diff_flag)
margins_selfish4_se_diff_flag <- margins(model_selfish4_se_diff_flag, variables = "treat_flag")
summary(margins_selfish4_se_diff_flag)

# 5. selfish5_se_diff ~ treat_flag
model_selfish5_se_diff_flag <- lm(selfish5_se_diff ~ treat_flag, data = df)
summary(model_selfish5_se_diff_flag)
margins_selfish5_se_diff_flag <- margins(model_selfish5_se_diff_flag, variables = "treat_flag")
summary(margins_selfish5_se_diff_flag)

# 6. selfish6_se_diff ~ treat_flag
model_selfish6_se_diff_flag <- lm(selfish6_se_diff ~ treat_flag, data = df)
summary(model_selfish6_se_diff_flag)
margins_selfish6_se_diff_flag <- margins(model_selfish6_se_diff_flag, variables = "treat_flag")
summary(margins_selfish6_se_diff_flag)

# 7. selfish7_se_diff ~ treat_flag
model_selfish7_se_diff_flag <- lm(selfish7_se_diff ~ treat_flag, data = df)
summary(model_selfish7_se_diff_flag)
margins_selfish7_se_diff_flag <- margins(model_selfish7_se_diff_flag, variables = "treat_flag")
summary(margins_selfish7_se_diff_flag)

# 8. selfish8_se_diff ~ treat_flag
model_selfish8_se_diff_flag <- lm(selfish8_se_diff ~ treat_flag, data = df)
summary(model_selfish8_se_diff_flag)
margins_selfish8_se_diff_flag <- margins(model_selfish8_se_diff_flag, variables = "treat_flag")
summary(margins_selfish8_se_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_flag_diff <- list(
  broom::tidy(model_selfish1_se_diff_flag) %>% mutate(outcome = "S"),
  broom::tidy(model_selfish2_se_diff_flag) %>% mutate(outcome = "V"),
  broom::tidy(model_selfish3_se_diff_flag) %>% mutate(outcome = "MP"),
  broom::tidy(model_selfish4_se_diff_flag) %>% mutate(outcome = "C"),
  broom::tidy(model_selfish5_se_diff_flag) %>% mutate(outcome = "L"),
  broom::tidy(model_selfish6_se_diff_flag) %>% mutate(outcome = "M"),
  broom::tidy(model_selfish7_se_diff_flag) %>% mutate(outcome = "KD"),
  broom::tidy(model_selfish8_se_diff_flag) %>% mutate(outcome = "SD")
)

coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
library(ggplot2)

flag_t3_se <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Selfish by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag selfish by party SE.jpeg",
  plot     = flag_t3_se
)

########################################################################

#MEAN

# 1. mean1_se_diff ~ treat_flag
model_mean1_se_diff_flag <- lm(mean1_se_diff ~ treat_flag, data = df)
summary(model_mean1_se_diff_flag)
margins_mean1_se_diff_flag <- margins(model_mean1_se_diff_flag, variables = "treat_flag")
summary(margins_mean1_se_diff_flag)

# 2. mean2_se_diff ~ treat_flag
model_mean2_se_diff_flag <- lm(mean2_se_diff ~ treat_flag, data = df)
summary(model_mean2_se_diff_flag)
margins_mean2_se_diff_flag <- margins(model_mean2_se_diff_flag, variables = "treat_flag")
summary(margins_mean2_se_diff_flag)

# 3. mean3_se_diff ~ treat_flag
model_mean3_se_diff_flag <- lm(mean3_se_diff ~ treat_flag, data = df)
summary(model_mean3_se_diff_flag)
margins_mean3_se_diff_flag <- margins(model_mean3_se_diff_flag, variables = "treat_flag")
summary(margins_mean3_se_diff_flag)

# 4. mean4_se_diff ~ treat_flag
model_mean4_se_diff_flag <- lm(mean4_se_diff ~ treat_flag, data = df)
summary(model_mean4_se_diff_flag)
margins_mean4_se_diff_flag <- margins(model_mean4_se_diff_flag, variables = "treat_flag")
summary(margins_mean4_se_diff_flag)

# 5. mean5_se_diff ~ treat_flag
model_mean5_se_diff_flag <- lm(mean5_se_diff ~ treat_flag, data = df)
summary(model_mean5_se_diff_flag)
margins_mean5_se_diff_flag <- margins(model_mean5_se_diff_flag, variables = "treat_flag")
summary(margins_mean5_se_diff_flag)

# 6. mean6_se_diff ~ treat_flag
model_mean6_se_diff_flag <- lm(mean6_se_diff ~ treat_flag, data = df)
summary(model_mean6_se_diff_flag)
margins_mean6_se_diff_flag <- margins(model_mean6_se_diff_flag, variables = "treat_flag")
summary(margins_mean6_se_diff_flag)

# 7. mean7_se_diff ~ treat_flag
model_mean7_se_diff_flag <- lm(mean7_se_diff ~ treat_flag, data = df)
summary(model_mean7_se_diff_flag)
margins_mean7_se_diff_flag <- margins(model_mean7_se_diff_flag, variables = "treat_flag")
summary(margins_mean7_se_diff_flag)

# 8. mean8_se_diff ~ treat_flag
model_mean8_se_diff_flag <- lm(mean8_se_diff ~ treat_flag, data = df)
summary(model_mean8_se_diff_flag)
margins_mean8_se_diff_flag <- margins(model_mean8_se_diff_flag, variables = "treat_flag")
summary(margins_mean8_se_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_flag_diff <- list(
  broom::tidy(model_mean1_se_diff_flag) %>% mutate(outcome = "S"),
  broom::tidy(model_mean2_se_diff_flag) %>% mutate(outcome = "V"),
  broom::tidy(model_mean3_se_diff_flag) %>% mutate(outcome = "MP"),
  broom::tidy(model_mean4_se_diff_flag) %>% mutate(outcome = "C"),
  broom::tidy(model_mean5_se_diff_flag) %>% mutate(outcome = "L"),
  broom::tidy(model_mean6_se_diff_flag) %>% mutate(outcome = "M"),
  broom::tidy(model_mean7_se_diff_flag) %>% mutate(outcome = "KD"),
  broom::tidy(model_mean8_se_diff_flag) %>% mutate(outcome = "SD")
)

coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---
library(ggplot2)

flag_t4_se <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Mean by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag mean by party SE.jpeg",
  plot     = flag_t4_se
)

###############################################################
#CAKE
################################################################


#SWEDEN

# INTELLIGENT

# 1. intelligent1_se_diff ~ treat_cake
model_intelligent1_se_diff_cake <- lm(intelligent1_se_diff ~ treat_cake, data = df)
summary(model_intelligent1_se_diff_cake)
margins_intelligent1_se_diff_cake <- margins(model_intelligent1_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent1_se_diff_cake)

# 2. intelligent2_se_diff ~ treat_cake
model_intelligent2_se_diff_cake <- lm(intelligent2_se_diff ~ treat_cake, data = df)
summary(model_intelligent2_se_diff_cake)
margins_intelligent2_se_diff_cake <- margins(model_intelligent2_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent2_se_diff_cake)

# 3. intelligent3_se_diff ~ treat_cake
model_intelligent3_se_diff_cake <- lm(intelligent3_se_diff ~ treat_cake, data = df)
summary(model_intelligent3_se_diff_cake)
margins_intelligent3_se_diff_cake <- margins(model_intelligent3_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent3_se_diff_cake)

# 4. intelligent4_se_diff ~ treat_cake
model_intelligent4_se_diff_cake <- lm(intelligent4_se_diff ~ treat_cake, data = df)
summary(model_intelligent4_se_diff_cake)
margins_intelligent4_se_diff_cake <- margins(model_intelligent4_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent4_se_diff_cake)

# 5. intelligent5_se_diff ~ treat_cake
model_intelligent5_se_diff_cake <- lm(intelligent5_se_diff ~ treat_cake, data = df)
summary(model_intelligent5_se_diff_cake)
margins_intelligent5_se_diff_cake <- margins(model_intelligent5_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent5_se_diff_cake)

# 6. intelligent6_se_diff ~ treat_cake
model_intelligent6_se_diff_cake <- lm(intelligent6_se_diff ~ treat_cake, data = df)
summary(model_intelligent6_se_diff_cake)
margins_intelligent6_se_diff_cake <- margins(model_intelligent6_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent6_se_diff_cake)

# 7. intelligent7_se_diff ~ treat_cake
model_intelligent7_se_diff_cake <- lm(intelligent7_se_diff ~ treat_cake, data = df)
summary(model_intelligent7_se_diff_cake)
margins_intelligent7_se_diff_cake <- margins(model_intelligent7_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent7_se_diff_cake)

# 8. intelligent8_se_diff ~ treat_cake
model_intelligent8_se_diff_cake <- lm(intelligent8_se_diff ~ treat_cake, data = df)
summary(model_intelligent8_se_diff_cake)
margins_intelligent8_se_diff_cake <- margins(model_intelligent8_se_diff_cake, variables = "treat_cake")
summary(margins_intelligent8_se_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_cake_diff <- list(
  broom::tidy(model_intelligent1_se_diff_cake) %>% mutate(outcome = "S"),
  broom::tidy(model_intelligent2_se_diff_cake) %>% mutate(outcome = "V"),
  broom::tidy(model_intelligent3_se_diff_cake) %>% mutate(outcome = "MP"),
  broom::tidy(model_intelligent4_se_diff_cake) %>% mutate(outcome = "C"),
  broom::tidy(model_intelligent5_se_diff_cake) %>% mutate(outcome = "L"),
  broom::tidy(model_intelligent6_se_diff_cake) %>% mutate(outcome = "M"),
  broom::tidy(model_intelligent7_se_diff_cake) %>% mutate(outcome = "KD"),
  broom::tidy(model_intelligent8_se_diff_cake) %>% mutate(outcome = "SD")
)

coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
library(ggplot2)

cake_t1_se <- ggplot(coef_df_cake_diff, aes(x = estimate, y = outcome)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.2) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  labs(
    title = "Cake: Intelligent by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake intelligent by party SE.jpeg",
  plot     = cake_t1_se
)
#############################################################

#RELIABLE

# reliable

# 1. reliable1_se_diff ~ treat_cake
model_reliable1_se_diff_cake <- lm(reliable1_se_diff ~ treat_cake, data = df)
summary(model_reliable1_se_diff_cake)
margins_reliable1_se_diff_cake <- margins(model_reliable1_se_diff_cake, variables = "treat_cake")
summary(margins_reliable1_se_diff_cake)

# 2. reliable2_se_diff ~ treat_cake
model_reliable2_se_diff_cake <- lm(reliable2_se_diff ~ treat_cake, data = df)
summary(model_reliable2_se_diff_cake)
margins_reliable2_se_diff_cake <- margins(model_reliable2_se_diff_cake, variables = "treat_cake")
summary(margins_reliable2_se_diff_cake)

# 3. reliable3_se_diff ~ treat_cake
model_reliable3_se_diff_cake <- lm(reliable3_se_diff ~ treat_cake, data = df)
summary(model_reliable3_se_diff_cake)
margins_reliable3_se_diff_cake <- margins(model_reliable3_se_diff_cake, variables = "treat_cake")
summary(margins_reliable3_se_diff_cake)

# 4. reliable4_se_diff ~ treat_cake
model_reliable4_se_diff_cake <- lm(reliable4_se_diff ~ treat_cake, data = df)
summary(model_reliable4_se_diff_cake)
margins_reliable4_se_diff_cake <- margins(model_reliable4_se_diff_cake, variables = "treat_cake")
summary(margins_reliable4_se_diff_cake)

# 5. reliable5_se_diff ~ treat_cake
model_reliable5_se_diff_cake <- lm(reliable5_se_diff ~ treat_cake, data = df)
summary(model_reliable5_se_diff_cake)
margins_reliable5_se_diff_cake <- margins(model_reliable5_se_diff_cake, variables = "treat_cake")
summary(margins_reliable5_se_diff_cake)

# 6. reliable6_se_diff ~ treat_cake
model_reliable6_se_diff_cake <- lm(reliable6_se_diff ~ treat_cake, data = df)
summary(model_reliable6_se_diff_cake)
margins_reliable6_se_diff_cake <- margins(model_reliable6_se_diff_cake, variables = "treat_cake")
summary(margins_reliable6_se_diff_cake)

# 7. reliable7_se_diff ~ treat_cake
model_reliable7_se_diff_cake <- lm(reliable7_se_diff ~ treat_cake, data = df)
summary(model_reliable7_se_diff_cake)
margins_reliable7_se_diff_cake <- margins(model_reliable7_se_diff_cake, variables = "treat_cake")
summary(margins_reliable7_se_diff_cake)

# 8. reliable8_se_diff ~ treat_cake
model_reliable8_se_diff_cake <- lm(reliable8_se_diff ~ treat_cake, data = df)
summary(model_reliable8_se_diff_cake)
margins_reliable8_se_diff_cake <- margins(model_reliable8_se_diff_cake, variables = "treat_cake")
summary(margins_reliable8_se_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_cake_diff <- list(
  broom::tidy(model_reliable1_se_diff_cake) %>% mutate(outcome = "S"),
  broom::tidy(model_reliable2_se_diff_cake) %>% mutate(outcome = "V"),
  broom::tidy(model_reliable3_se_diff_cake) %>% mutate(outcome = "MP"),
  broom::tidy(model_reliable4_se_diff_cake) %>% mutate(outcome = "C"),
  broom::tidy(model_reliable5_se_diff_cake) %>% mutate(outcome = "L"),
  broom::tidy(model_reliable6_se_diff_cake) %>% mutate(outcome = "M"),
  broom::tidy(model_reliable7_se_diff_cake) %>% mutate(outcome = "KD"),
  broom::tidy(model_reliable8_se_diff_cake) %>% mutate(outcome = "SD")
)

coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
library(ggplot2)

cake_t2_se <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Reliable by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake reliable by party SE.jpeg",
  plot     = cake_t2_se
)

####################################################

#SELFISH
# selfish

# 1. selfish1_se_diff ~ treat_cake
model_selfish1_se_diff_cake <- lm(selfish1_se_diff ~ treat_cake, data = df)
summary(model_selfish1_se_diff_cake)
margins_selfish1_se_diff_cake <- margins(model_selfish1_se_diff_cake, variables = "treat_cake")
summary(margins_selfish1_se_diff_cake)

# 2. selfish2_se_diff ~ treat_cake
model_selfish2_se_diff_cake <- lm(selfish2_se_diff ~ treat_cake, data = df)
summary(model_selfish2_se_diff_cake)
margins_selfish2_se_diff_cake <- margins(model_selfish2_se_diff_cake, variables = "treat_cake")
summary(margins_selfish2_se_diff_cake)

# 3. selfish3_se_diff ~ treat_cake
model_selfish3_se_diff_cake <- lm(selfish3_se_diff ~ treat_cake, data = df)
summary(model_selfish3_se_diff_cake)
margins_selfish3_se_diff_cake <- margins(model_selfish3_se_diff_cake, variables = "treat_cake")
summary(margins_selfish3_se_diff_cake)

# 4. selfish4_se_diff ~ treat_cake
model_selfish4_se_diff_cake <- lm(selfish4_se_diff ~ treat_cake, data = df)
summary(model_selfish4_se_diff_cake)
margins_selfish4_se_diff_cake <- margins(model_selfish4_se_diff_cake, variables = "treat_cake")
summary(margins_selfish4_se_diff_cake)

# 5. selfish5_se_diff ~ treat_cake
model_selfish5_se_diff_cake <- lm(selfish5_se_diff ~ treat_cake, data = df)
summary(model_selfish5_se_diff_cake)
margins_selfish5_se_diff_cake <- margins(model_selfish5_se_diff_cake, variables = "treat_cake")
summary(margins_selfish5_se_diff_cake)

# 6. selfish6_se_diff ~ treat_cake
model_selfish6_se_diff_cake <- lm(selfish6_se_diff ~ treat_cake, data = df)
summary(model_selfish6_se_diff_cake)
margins_selfish6_se_diff_cake <- margins(model_selfish6_se_diff_cake, variables = "treat_cake")
summary(margins_selfish6_se_diff_cake)

# 7. selfish7_se_diff ~ treat_cake
model_selfish7_se_diff_cake <- lm(selfish7_se_diff ~ treat_cake, data = df)
summary(model_selfish7_se_diff_cake)
margins_selfish7_se_diff_cake <- margins(model_selfish7_se_diff_cake, variables = "treat_cake")
summary(margins_selfish7_se_diff_cake)

# 8. selfish8_se_diff ~ treat_cake
model_selfish8_se_diff_cake <- lm(selfish8_se_diff ~ treat_cake, data = df)
summary(model_selfish8_se_diff_cake)
margins_selfish8_se_diff_cake <- margins(model_selfish8_se_diff_cake, variables = "treat_cake")
summary(margins_selfish8_se_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_cake_diff <- list(
  broom::tidy(model_selfish1_se_diff_cake) %>% mutate(outcome = "S"),
  broom::tidy(model_selfish2_se_diff_cake) %>% mutate(outcome = "V"),
  broom::tidy(model_selfish3_se_diff_cake) %>% mutate(outcome = "MP"),
  broom::tidy(model_selfish4_se_diff_cake) %>% mutate(outcome = "C"),
  broom::tidy(model_selfish5_se_diff_cake) %>% mutate(outcome = "L"),
  broom::tidy(model_selfish6_se_diff_cake) %>% mutate(outcome = "M"),
  broom::tidy(model_selfish7_se_diff_cake) %>% mutate(outcome = "KD"),
  broom::tidy(model_selfish8_se_diff_cake) %>% mutate(outcome = "SD")
)

coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
library(ggplot2)

cake_t3_se <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Selfish by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake selfish by party SE.jpeg",
  plot     = cake_t3_se
)

########################################################################

#MEAN

# 1. mean1_se_diff ~ treat_cake
model_mean1_se_diff_cake <- lm(mean1_se_diff ~ treat_cake, data = df)
summary(model_mean1_se_diff_cake)
margins_mean1_se_diff_cake <- margins(model_mean1_se_diff_cake, variables = "treat_cake")
summary(margins_mean1_se_diff_cake)

# 2. mean2_se_diff ~ treat_cake
model_mean2_se_diff_cake <- lm(mean2_se_diff ~ treat_cake, data = df)
summary(model_mean2_se_diff_cake)
margins_mean2_se_diff_cake <- margins(model_mean2_se_diff_cake, variables = "treat_cake")
summary(margins_mean2_se_diff_cake)

# 3. mean3_se_diff ~ treat_cake
model_mean3_se_diff_cake <- lm(mean3_se_diff ~ treat_cake, data = df)
summary(model_mean3_se_diff_cake)
margins_mean3_se_diff_cake <- margins(model_mean3_se_diff_cake, variables = "treat_cake")
summary(margins_mean3_se_diff_cake)

# 4. mean4_se_diff ~ treat_cake
model_mean4_se_diff_cake <- lm(mean4_se_diff ~ treat_cake, data = df)
summary(model_mean4_se_diff_cake)
margins_mean4_se_diff_cake <- margins(model_mean4_se_diff_cake, variables = "treat_cake")
summary(margins_mean4_se_diff_cake)

# 5. mean5_se_diff ~ treat_cake
model_mean5_se_diff_cake <- lm(mean5_se_diff ~ treat_cake, data = df)
summary(model_mean5_se_diff_cake)
margins_mean5_se_diff_cake <- margins(model_mean5_se_diff_cake, variables = "treat_cake")
summary(margins_mean5_se_diff_cake)

# 6. mean6_se_diff ~ treat_cake
model_mean6_se_diff_cake <- lm(mean6_se_diff ~ treat_cake, data = df)
summary(model_mean6_se_diff_cake)
margins_mean6_se_diff_cake <- margins(model_mean6_se_diff_cake, variables = "treat_cake")
summary(margins_mean6_se_diff_cake)

# 7. mean7_se_diff ~ treat_cake
model_mean7_se_diff_cake <- lm(mean7_se_diff ~ treat_cake, data = df)
summary(model_mean7_se_diff_cake)
margins_mean7_se_diff_cake <- margins(model_mean7_se_diff_cake, variables = "treat_cake")
summary(margins_mean7_se_diff_cake)

# 8. mean8_se_diff ~ treat_cake
model_mean8_se_diff_cake <- lm(mean8_se_diff ~ treat_cake, data = df)
summary(model_mean8_se_diff_cake)
margins_mean8_se_diff_cake <- margins(model_mean8_se_diff_cake, variables = "treat_cake")
summary(margins_mean8_se_diff_cake)


# --- Bygg upp dataframen för treat_cake (_diff-modellerna) ---
library(broom)
library(dplyr)

coef_list_cake_diff <- list(
  broom::tidy(model_mean1_se_diff_cake) %>% mutate(outcome = "S"),
  broom::tidy(model_mean2_se_diff_cake) %>% mutate(outcome = "V"),
  broom::tidy(model_mean3_se_diff_cake) %>% mutate(outcome = "MP"),
  broom::tidy(model_mean4_se_diff_cake) %>% mutate(outcome = "C"),
  broom::tidy(model_mean5_se_diff_cake) %>% mutate(outcome = "L"),
  broom::tidy(model_mean6_se_diff_cake) %>% mutate(outcome = "M"),
  broom::tidy(model_mean7_se_diff_cake) %>% mutate(outcome = "KD"),
  broom::tidy(model_mean8_se_diff_cake) %>% mutate(outcome = "SD")
)

coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")
coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---
library(ggplot2)

cake_t4_se <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Mean by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake mean by party SE.jpeg",
  plot     = cake_t4_se
)